﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Resources;
using System.Text;
using System.Windows.Forms;
using SMS.cntr;
using SMS.dao;
using SMS.util;

namespace SMS.view
{
    public partial class PreferenceForm : Form
    {
        public PreferenceForm()
        {
            InitializeComponent();
        }

        //long

        private String code;

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            PreferenceCntr.Instance.update(txtValue.Text, code);
            bindDataToGridViewPreference();
            txtValue.Text = "";
        }

        private void PrefenceForm_Load(object sender, EventArgs e)
        {
            bindDataToGridViewPreference();
            changeLang(I18nUtil.vnLang);
        }

        private void bindDataToGridViewPreference()
        {
            gridViewPreference.Rows.Clear();
            DataTable table = PreferenceCntr.Instance.findAll();

            for (int i = 0; i < table.Rows.Count; i++)
            {
                gridViewPreference.Rows.Add(table.Rows[i]["pre_code"].ToString(),
                                           table.Rows[i]["pre_name"].ToString(),
                                           table.Rows[i]["pre_value"].ToString(),
                                           table.Rows[i]["pre_id"].ToString());
                gridViewPreference.Rows[gridViewPreference.RowCount - 1].Tag = table.Rows[i];
            }
            gridViewPreference.ClearSelection();
        }

        private void gridViewPreference_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            for (int i = 0; i < gridViewPreference.SelectedRows.Count; i++)
            {
                int id = Int32.Parse(gridViewPreference.SelectedRows[i].Cells[3].Value.ToString());
                code = PreferenceDao.Instance.findById(id)[0].ToString();
                String name = PreferenceDao.Instance.findById(id)[1].ToString();
                String value = PreferenceDao.Instance.findById(id)[2].ToString();
                txtName.Text = name;
                txtValue.Text = value;
            }
        }

        public void changeLang(String lang)
        {
            I18nUtil.init(lang, new ResourceManager(I18nUtil.resourcePath, typeof(PreferenceForm).Assembly));

            this.Text = I18nUtil.getString("preference");
            label1.Text = I18nUtil.getString("lblPreference");
            label2.Text = I18nUtil.getString("lblValue");
            btnUpdate.Text = I18nUtil.getString("btnUpdate");
            groupBox1.Text = I18nUtil.getString("gbPreferenceList");
            gridViewPreference.Columns[0].HeaderText = I18nUtil.getString("code");
            gridViewPreference.Columns[1].HeaderText = I18nUtil.getString("lblPreference");
            gridViewPreference.Columns[2].HeaderText = I18nUtil.getString("lblValue");
        }

        //end of long
        
    }
}
